﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace GZip.Common
{
    public class GZipHelper
    {




        const string Sign_Chunked_End = "\r\n";
        const string Sign_Chunked = "chunked";


        public static byte[] ParseChunkedData(byte[] data)
        {
            int endPosition = data.Length;

            List<byte> list = new List<byte>();


            using (MemoryStream stream = new MemoryStream(data))
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    StringBuilder line = new StringBuilder();

                    while (stream.Position < endPosition)
                    {
                        line.Append(reader.ReadChar());

                        string lineString = line.ToString();

                        int inx = lineString.IndexOf(Sign_Chunked_End);

                        if (inx > 0)
                        {
                            int count = Convert.ToInt32(lineString.Substring(0, inx), 16);

                            if (count > 0)
                            {
                                byte[] bytes = reader.ReadBytes(count);
                                if (bytes != null)
                                {
                                    list.AddRange(bytes);
                                }
                            }

                            line.Clear();
                        }
                        else if (inx == 0)
                        {
                            break;
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                }

                stream.Close();
                stream.Dispose();
            }


            return list.ToArray();
        }

   


        /// <summary>
        /// GZip解压函数
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GZipDecompress(byte[] data)
        {


            int bufferSize = 256;
            

            using (MemoryStream stream = new MemoryStream())
            {
                using (GZipStream gZipStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress))
                {
                    byte[] bytes = new byte[bufferSize];
                    int n;
                    while ((n = gZipStream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        stream.Write(bytes, 0, n);
                    }
                    gZipStream.Close();
                    gZipStream.Dispose();
                }

                byte[] rdata = stream.ToArray();

                stream.Close();
                stream.Dispose();

                return rdata;
            }
        }
        /// <summary>
        /// GZip压缩函数
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] GZipCompress(byte[] data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
                {
                    gZipStream.Write(data, 0, data.Length);
                    gZipStream.Close();
                    gZipStream.Dispose();
                }
                return stream.ToArray();
            }
        }
    }
}
